/*
* pps_bom
*/
package com.pitop.bom.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.bom.entity.BomDetail;
import com.pitop.bom.entity.BomDetailLog;
import com.pitop.bom.entity.BomDetailLogState;
import com.pitop.bom.service.BomDetailLogSV;
import com.pitop.bom.service.BomDetailSV;
import com.pitop.bom.vo.BomDetailVO;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * bom明细 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/bomDetail")
@Slf4j
@Api(value = "bom明细控制器", description = "bom明细控制器")
public class BomDetailCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private BomDetailSV bomDetailSV;

    @Resource
    private BomDetailLogSV bomDetailLogSV;

    /**
     * 查询BomDetail一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BomDetail load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        BomDetail bomDetail = bomDetailSV.load(id, code);
        log.info(JSON.toJSONString(bomDetail));
        return bomDetail;
    }

    /**
     * 查询BomDetail一个详情信息，(将查询关联数据)
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BomDetail get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        BomDetail bomDetail = bomDetailSV.get(id, code);
        log.info(JSON.toJSONString(bomDetail));
        return bomDetail;
    }

    /**
     * 根据条件id查询BomDetail一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BomDetail loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        BomDetail bomDetail = bomDetailSV.loadById(id);
        log.info(JSON.toJSONString(bomDetail));
        return bomDetail;
    }

    /**
     * 根据条件code查询BomDetail一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BomDetail loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        BomDetail bomDetail = bomDetailSV.loadByCode(code);
        log.info(JSON.toJSONString(bomDetail));
        return bomDetail;
    }

    /**
     * 根据条件id查询BomDetail一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BomDetail getById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        BomDetail bomDetail = bomDetailSV.getById(id);
        log.info(JSON.toJSONString(bomDetail));
        return bomDetail;
    }

    /**
     * 根据条件code查询BomDetail一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public BomDetail getByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        BomDetail bomDetail = bomDetailSV.getByCode(code);
        log.info(JSON.toJSONString(bomDetail));
        return bomDetail;
    }

    /**
     * 查询BomDetail信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询BomDetail信息集合", notes = "查询BomDetail信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom编号",  paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型",  paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态",  paramType = "query"),
            @ApiImplicitParam(name = "name", value = "零件名",  paramType = "query"),
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<BomDetail> list(@RequestBody @ApiIgnore BomDetail bomDetail, Integer curPage, Integer pageSize) {
        Page<BomDetail> page = new Page<BomDetail>(pageSize, curPage);
        List<BomDetail> bomDetails = bomDetailSV.list(bomDetail, page.genRowStart(), page.getPageSize());
        int total = bomDetailSV.count(bomDetail);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return bomDetails;
    }

    /**
     * 查询BomDetail信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询BomDetail信息集合", notes = "查询BomDetail信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<BomDetail> listByPk(Long id, String code, Integer curPage, Integer pageSize) {
        Page<BomDetail> page = new Page<BomDetail>(pageSize, curPage);
        List<BomDetail> bomDetails = bomDetailSV.list(id, code, page.genRowStart(), page.getPageSize());
        int total = bomDetailSV.count(id, code);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return bomDetails;
    }

    /**
     * 统计BomDetail信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计BomDetail信息数量根据主键", notes = " 统计BomDetail信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String code) {
        return bomDetailSV.count(id, code);
    }

    /**
     * 统计BomDetail信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计BomDetail信息数量", notes = "统计BomDetail信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
            @ApiImplicitParam(name = "supplierCode", value = "供应商编码", paramType = "query"),
            @ApiImplicitParam(name = "materialAssemblyCode", value = "物料或组件编码", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型[物料 Material ,组件 Assembly]", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：[开发Develop，稳定Stable，停用 Stop]", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "零件名", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "用量", paramType = "query"),
            @ApiImplicitParam(name = "position", value = "位置", paramType = "query"),
            @ApiImplicitParam(name = "material", value = "材料", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody BomDetail bomDetail) {
        return bomDetailSV.count(bomDetail);
    }

    /**
     * 创建BomDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建BomDetail", notes = "创建BomDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
            @ApiImplicitParam(name = "materialAssemblyCode", value = "物料或组件编码", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型[物料 Material ,组件 Assembly]", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "零件名", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "用量", paramType = "query"),
            @ApiImplicitParam(name = "position", value = "位置", paramType = "query"),
            @ApiImplicitParam(name = "material", value = "材料", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "管理员账户", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "管理员账号编码", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public BomDetail build(@RequestBody @ApiIgnore BomDetail bomDetail, String account, String code) {
        bomDetailSV.save(bomDetail, account, code);
        return bomDetail;
    }

    /**
     * 创建BomDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建BomDetail", notes = "创建BomDetail")
    @PostMapping("/save")
    @ResponseBody
    public BomDetail save(@RequestBody List<BomDetailVO> bomDetailVOs, String account, String code) {
        bomDetailSV.save(bomDetailVOs, account, code);
        return null;
    }

    /**
     * 修改BomDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改BomDetail", notes = "修改BomDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "零件名", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "用量", paramType = "query"),
            @ApiImplicitParam(name = "position", value = "位置", paramType = "query"),
            @ApiImplicitParam(name = "material", value = "材料", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BomDetail modify(@RequestBody @ApiIgnore BomDetail bomDetail, String account, String code) {
        bomDetailSV.modify(bomDetail);
        //添加修改日志
        BomDetailLog bomDetailLog = new BomDetailLog();
        bomDetailLog.setBomDetailCode(bomDetail.getCode());
        bomDetailLog.setOperateName(account);
        bomDetailLog.setOperateCode(code);
        bomDetailLog.setEvent(BomDetailLogState.Modify.name());
        bomDetailLog.setCreateTime(new Date());
        bomDetailLog.setSummary("管理员" + account +"修改了bom明细" + bomDetail.getName());
        bomDetailLogSV.save(bomDetailLog);
        return bomDetail;
    }

    /**
     * 删除BomDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除BomDetail", notes = "删除BomDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        bomDetailSV.delete(id, code);
        return BeanRet.create(true, "删除BomDetail成功");
    }

    /**
     * 修改bom详情状态
     *
     * @param code  组件编码
     * @param state 状态
     * @return
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态(1032):Develop(开发) Stable(稳定) Stop(停用)", paramType = "query", required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, String state) {
        if (StringUtils.isEmpty(code) ||StringUtils.isEmpty(state)  ) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        bomDetailSV.updateState(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

}
